<?php
class Shop_Model_SelectRail extends Shop_Model_Base{

	/**
	 * 表示用変換
	 * @param $railList
	 * @param $params
	 * @return 変換後リスト
	 */
	public function convertData($railList, $params)
	{
		$DispList = array();
		$indexNo = 0;
		$beforePrefCd = null;
		$beforeEnsenKbn = null;

		foreach ($railList as $railData)
		{
			$prefCd = $railData['TODOUFUKEN_CD'];
			$ensenKbn = $railData['ENSEN_KBN'];

			// 都道府県が異なる場合
			if ($beforePrefCd != $prefCd)
			{
				$DispList["$prefCd"]['PrefName'] = $railData['TODOUFUKEN_NAME'];
				$DispList["$prefCd"]['EnsenKbnList'] = array();
				$beforeEnsenKbn = null;
			}
			
			// 沿線区分が異なる場合
			if ($beforeEnsenKbn != $ensenKbn)
			{
				$ensenKbnName = Apm_ShopUtil::getEnsenKbnName($ensenKbn);
				
				$DispList["$prefCd"]['EnsenKbnList'][] = array(
					'EnsenKbn'	 => $ensenKbn,
					'EnsenKbnName' => $ensenKbnName,
					'RailList'	 => array()
				);
				$indexNo = count($DispList["$prefCd"]['EnsenKbnList']) - 1;
			}

			if (!empty($params['RailCd']) && in_array($railData["ENSEN_CD"], $params['RailCd']))
			{
				$railData['checked'] = "checked='checked'";
			}
			
			$DispList["$prefCd"]['EnsenKbnList'][$indexNo]['RailList'][] = $railData;
			
			$beforePrefCd = $prefCd;
			$beforeEnsenKbn = $ensenKbn;
		}
		
		return $DispList;
	}

	/**
	 * 表示用変換（API用）
	 * @param $railList
	 */
	public function convertMobileData(&$railList)
	{
		for ($index = 0; $index < count($railList); $index++)
		{
			$ensenKbnName = '';

			switch ($railList[$index]['ENSEN_KBN'])
			{
				case Apm_Config_Shop::ENSEN_KUBUN_JR :
					$ensenKbnName = Apm_Config_Shop::ENSEN_KUBUN_NAME_JR;
					break;
				case Apm_Config_Shop::ENSEN_KUBUN_PRIVATE_RAILWAY :
					$ensenKbnName = Apm_Config_Shop::ENSEN_KUBUN_NAME_PRIVATE_RAILWAY;
					break;
				case Apm_Config_Shop::ENSEN_KUBUN_SUBWAY :
					$ensenKbnName = Apm_Config_Shop::ENSEN_KUBUN_NAME_SUBWAY;
					break;
				case Apm_Config_Shop::ENSEN_KUBUN_OTHER :
					$ensenKbnName = Apm_Config_Shop::ENSEN_KUBUN_NAME_OTHER;
					break;
				case Apm_Config_Shop::ENSEN_KUBUN_BUS :
					$ensenKbnName = Apm_Config_Shop::ENSEN_KUBUN_NAME_BUS;
					break;
				case Apm_Config_Shop::ENSEN_KUBUN_OTHER_TRANSPORTATION :
					$ensenKbnName = Apm_Config_Shop::ENSEN_KUBUN_NAME_OTHER_TRANSPORTATION;
					break;
			}
			$railList[$index]['ENSEN_KBN_NAME'] = $ensenKbnName;
		}
	}

	/**
	 * 店舗を探す（沿線から探す）sql文生成
	 * sql文生成
	 * @param $params
	 * 		地図から検索から遷移：都道府県CD（必須）
	 * 		サービス内容から検索から遷移：都道府県CD（任意）、サービス内容（任意）
	 * @param $db_params
	 * @return sql文
	 */
	protected function generateSql($params, &$db_params)
	{
		$strCondPrefM03 = '';
		$strCondPrefM05 = '';
		$strQryWhere = '';
		
		// 条件文生成
		$PrefCdList = array();
		
		// 都道府県コードが設定されている場合
		if (!empty($params['PrefCd']))
		{
			$PrefCdList = array_filter($params['PrefCd']);
		}
		
		// 都道府県コードが存在する場合
		if (count($PrefCdList) > 0)
		{
			$strCondPrefM03 .= ' AND M03_TODOUFUKEN_CD ' . $this->generateWhereIn("prefCdM03",       $PrefCdList, $db_params);
			$strCondPrefM05 .= ' AND M05_TENPO_TODOUFUKEN_CD ' . $this->generateWhereIn("prefCdM05", $PrefCdList, $db_params);
		}
		
		// サービス内容から検索
		$strQryWhere .= $this->generateCondService($params);
		
		// ダミー店舗除外
		$strQryWhere .= $this->generateNotinDummyShop("M05_TENPO_CD", $db_params);

// SQL生成
$sql = <<< DOC_END
SELECT
		 M05_TENPO_TODOUFUKEN_CD AS TODOUFUKEN_CD
		,M43_TODOUFUKEN AS TODOUFUKEN_NAME
		,M02_ENSEN_CD AS ENSEN_CD
		,M02_ENSEN_NAME AS ENSEN_NAME
		,M02_ENSEN_KANA AS ENSEN_KANA
		,M02_ENSEN_KUBUN AS ENSEN_KBN
		,REC_CNT AS TENPO_COUNT
FROM
	 M43TODOUFUKEN G
	,(
		SELECT
			 M05_TENPO_TODOUFUKEN_CD
			,M02_ENSEN_NAME
			,M02_ENSEN_KANA
			,M02_ENSEN_CD
			,NVL(M02_ENSEN_KUBUN, '6') AS M02_ENSEN_KUBUN
			,COUNT(*) AS REC_CNT
		FROM
			 M02ENSEN E
			,(
				SELECT
					 M05_TENPO_CD
					,M05_TENPO_TODOUFUKEN_CD
					,M03_ENSEN_CD
				FROM
					(
						SELECT
							 M03_ENSEN_CD
							,M03_TODOUFUKEN_CD
							,M03_RENRAKU_EKI_CD
						FROM
							 M03EKI
						WHERE
							M03_RENRAKU_EKI_CD is not null
						{$strCondPrefM03}
					) A
					,(
						SELECT
							 M05_TENPO_CD
							,M05_TENPO_TODOUFUKEN_CD
							,M03_RENRAKU_EKI_CD as M05_RENRAKU_EKI_CD
						FROM
							 M05TENPO_INFO
							,M03EKI
						WHERE
							M05_EKI_CD01 = M03_EKI_CD
						AND M05_KOUKAI_FLAG = 1
						AND ( M05_TENPO_CD > '01000000' AND M05_TENPO_CD < '48000000' )
						AND M03_RENRAKU_EKI_CD is not null
						{$strCondPrefM03}
						{$strCondPrefM05}
						{$strQryWhere}
					) B
				WHERE
					M03_TODOUFUKEN_CD = M05_TENPO_TODOUFUKEN_CD
				AND M03_RENRAKU_EKI_CD = M05_RENRAKU_EKI_CD
			UNION
				SELECT
					 M05_TENPO_CD
					,M05_TENPO_TODOUFUKEN_CD
					,M03_ENSEN_CD
				FROM
					 M05TENPO_INFO
					,M03EKI
				WHERE
					M05_EKI_CD01 = M03_EKI_CD
				AND M05_KOUKAI_FLAG = 1
				AND ( M05_TENPO_CD > '01000000' AND M05_TENPO_CD < '48000000' )
				AND M03_RENRAKU_EKI_CD is null
				{$strCondPrefM03}
				{$strCondPrefM05}
				{$strQryWhere}
			) F
		WHERE
			E.M02_ENSEN_CD = F.M03_ENSEN_CD
		GROUP BY
			 F.M05_TENPO_TODOUFUKEN_CD
			,E.M02_ENSEN_NAME
			,E.M02_ENSEN_KANA
			,E.M02_ENSEN_CD
			,E.M02_ENSEN_KUBUN
	) H
WHERE
	G.M43_TODOUFUKEN_CD = H.M05_TENPO_TODOUFUKEN_CD
ORDER BY
	 G.M43_TODOUFUKEN_CD ASC
	,H.M02_ENSEN_KUBUN ASC
	,H.M02_ENSEN_CD ASC
DOC_END;

		return $sql;
	}

}
?>